<?php
class ml_model_dbILike extends Lib_datamodel_db
{
    const ML_ILIKE_STATUS_NORMAL = 1;
    const ML_ILIKE_STATUS_DELETE = 9;

    public function __construct()
    {
        $db_config = ml_factory::load_standard_conf('dbContent');
        parent::__construct('ilike' , $db_config['ilike']);
    }

    public function createLike($uid, $rid, $oid, $att, $from, $time =null)
    {
        if(!$this->init_db($uid, self::DB_MASTER))
        return false;

        $arrInsert = array(
            'uid' => $uid,
            'content_rid' => $rid,
            'owner_uid' => $oid,
            'attitude' => $att,
            'status' => self::ML_ILIKE_STATUS_NORMAL,
            'from' => $from,
            'cip' => Tool_ip::get_real_ip()
        );
        
        if (!empty($time)) {
            $arrInsert['ctime'] = $time;
        }

        return $this->insert($arrInsert);
    }

    /**
     *
     * 查找$num个我表达过$att态度的内容
     * @param unknown_type $uid
     * @param unknown_type $begin 起始位置，默认0
     * @param unknown_type $num 条目数，默认20，结尾-1
     * @param unknown_type $att 态度值，默认全部
     * @todo 筛选任意多状态值
     */
    public function getListByUid($uid, $begin = 0, $num = 20, $att)
    {
        if(!$this->init_db($uid, self::DB_SLAVE))
        return false;

        $fields = ' content_rid,owner_uid,attitude';
        $limit = " ORDER BY ctime DESC LIMIT $begin, $num ";
        $whereAtt = isset($att)?  " AND attitude = $att AND " : '';
        $whereAtt .= 'status = '.self::ML_ILIKE_STATUS_NORMAL;

        $sql = 'SELECT '. $fields. ' FROM '. $this->table . ' WHERE uid = '. $uid. $whereAtt. $limit;

        $rs = $this->fetch($sql);

        return $rs? $this->_data: false;

    }

    public function getListByUidRids($uid , $aRid)
    {
        if(!$this->init_db($uid, self::DB_SLAVE))
        return false;

        $sRid = '"'.implode('","' , $aRid).'"';
        $sql = 'select `content_rid`,`attitude` from '.$this->table.' where `uid`='.$uid.' and `content_rid` in ('.$sRid.')';
        $where .= ' AND status = '.self::ML_ILIKE_STATUS_NORMAL;

        return $this->fetch($sql);
    }

    public function getAttByRid($uid,$rid)
    {
        if(!$this->init_db($uid, self::DB_SLAVE))
        return false;

        $sql = 'select `attitude` from `'.$this->table.'` where `uid`='.$uid.' and `content_rid`= "'.$this->escape($rid).'"';
        $sql .= ' AND status = '.self::ML_ILIKE_STATUS_NORMAL;

        $rs=$this->fetch_row($sql);

        return $rs? $this->_data: false;
    }



    public function isIAttByRid($uid, $rid, &$result)
    {
        if(!$this->init_db($uid, self::DB_SLAVE))
        return false;

        $where = "content_rid = '{$this->escape($rid)}' AND uid = {$uid}";
        $where .= ' AND status = '.self::ML_ILIKE_STATUS_NORMAL;

        $rs = $this->fetch_count($where);
        if (!$rs) return false;
        $result = $this->_data > 0 ? true : false;
        return true;
    }

    public function isIAttByRids($uid, $aRid)
    {
        if(!$this->init_db($uid, self::DB_SLAVE))
        return false;

        $sRid = '"'.implode('","' , $aRid).'"';
        $sql = 'select `content_rid` from `'.$this->table.'` where `uid`='.$uid.' and `content_rid` in ('.$sRid.')';
        $sql .= ' AND status = '.self::ML_ILIKE_STATUS_NORMAL;

        $rs = $this->fetch($sql);
        if(!$rs)
        return false;
        //var_dump($this->get_data());
        $aRs = Tool_array::format_2d_array($this->get_data() , 'content_rid' , Tool_array::FORMAT_VALUE_ONLY );
        //var_dump($aRs);
        $aRs = array_fill_keys($aRs , 1);
        //var_dump($aRs);var_dump($aRid);die();
        //这里是已存在的 + 全部，数组相加会排重 重复时值使用前者的值 ，所以 1能保留
        if(empty($aRs)){
            $aRs=array();
        }
        $aRs = $aRs + array_fill_keys($aRid , 0);
        $this->set_data($aRs);
        return true;
    }

    public function getCountByUid($uid, $att)
    {
        if(!$this->init_db($uid, self::DB_SLAVE))
        return false;
        $att=$this->escape($att);
        $sql = 'SELECT COUNT(*) as `num` FROM '. $this->table. ' WHERE uid = '. $uid. ' AND attitude ='. $att;
        $sql .= ' AND status = '.self::ML_ILIKE_STATUS_NORMAL;

        $rs = $this->fetch($sql);
        return $rs? $this->_data[0]: false;
    }


    public function deleteLike($uid, $rid) {
        if(!$this->init_db($uid, self::DB_MASTER))
        return false;

        $data = array('status' => self::ML_ILIKE_STATUS_DELETE);
        $where = "uid =$uid and content_rid ='$rid'";

        return $this->update($data, $where);
        /**
         * @todo
         */
    }

    /**
     *
     * 输入uid数组，我喜欢的status置9
     * xinhua
     *
     */
    public function update_when_del_content($uids,$rid)
    {
        if(empty($uids)){
            return false;                                        //参数错误
        }

        $data=array();                                        //内容数据

        foreach ($uids as $uid) {

            $this->init_db($uid,self::DB_MASTER);
            $data = array('status' => self::ML_ILIKE_STATUS_DELETE);
            $where = "`uid` = '$uid' and `content_rid` = '$rid' ";
            $this->update($data, $where);

        }
        return true;
    }
}
